using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace JSG.BubbleShoot
{
    public class Player : MonoBehaviour
    {
        public Rigidbody2D m_Body;




        public SpriteRenderer m_Image;

        [HideInInspector]
        public bool m_Shot;

        [HideInInspector]
        public int m_ColorNum = 0;

        [HideInInspector]
        public int m_Hits;

        public int m_MaxHits = 5;

        public Sprite[] m_ColorSprites;

        public static Player m_Current;

        public GameObject m_DestroyParticle;
        public GameObject m_BounceParticle;

        public int m_PowerNum = 0;

        private void Awake()
        {
            m_Current = this;
        }
        // Start is called before the first frame update
        void Start()
        {
            m_Hits = 0;
            m_Shot = false;

            if (m_PowerNum == 0) //color ball
            {
                //SetColor(0);
                //SelectRandomColor();
            }

        }

        public void SelectRandomColor()
        {
            int lastColor = m_ColorNum;
            for (int i = 0; i < 20; i++)
            {
                int randomNum = Random.Range(0, 6);
                if (GameControl.m_Current.m_ColorCount[randomNum] != 0 && randomNum != lastColor)
                {
                    SetColor(randomNum);
                    break;
                }
            }
        }

        // Update is called once per frame
        void Update()
        {
            //if(m_Shot)
            //{
            //    if (m_Body.velocity.magnitude <= 30)
            //    {
            //        EndBall();
            //    }
            //}

        }

        public void SetColor(int num)
        {
            m_ColorNum = num;
            m_Image.sprite = m_ColorSprites[num];
        }

        private void OnCollisionEnter2D(Collision2D collision)
        {
            Vector3 pos = collision.contacts[0].point;
            m_Hits++;
            if (m_PowerNum == 0) //color ball
            {
                if (collision.gameObject.tag == "Candy")
                {
                    Candy c = collision.gameObject.GetComponent<Candy>();
                    ShakeAll();
                    //c.m_Sound.Play();
                    if (!c.m_GotHit && c.m_ColorNum == m_ColorNum)
                    {
                        SelectRandomColor();
                        c.Hit();
                    }
                }

                CameraControl.Current.StartShake(.2f, 2);

                GameObject obj = Instantiate(m_BounceParticle);
                obj.transform.position = pos + new Vector3(0, 0, -2);
                Destroy(obj, 3);
            }
            else if (m_PowerNum == 1)  //rainbow ball
            {
                if (collision.gameObject.tag == "Candy")
                {
                    Candy c = collision.gameObject.GetComponent<Candy>();
                    ShakeAll();
                    c.Hit();
                }

                CameraControl.Current.StartShake(.2f, 2);

                GameObject obj = Instantiate(m_BounceParticle);
                obj.transform.position = pos + new Vector3(0, 0, -2);
                Destroy(obj, 3);
            }
            else if (m_PowerNum == 2) //bomb
            {
                ExplodeBomb();
                EndBall();
                return;
            }

            if (m_Hits >= m_MaxHits)
            {
                EndBall();

                GameObject obj = Instantiate(m_DestroyParticle);
                obj.transform.position = transform.position + new Vector3(0, 0, -2);
                Destroy(obj, 3);
            }
        }

        public void ExplodeBomb()
        {
            CameraControl.Current.StartShake(.5f, 10);

            foreach (Candy candy in GameControl.m_Current.m_Candies)
            {
                if (!candy.gameObject.activeSelf)
                    continue;

                float dis = Vector3.Distance(candy.transform.position, transform.position);
                if (dis <= 25)
                {
                    candy.DestroyAlone();
                    //Vector3 dir = candy.transform.position - transform.position;
                    //dir.z = 0;
                    //dir.Normalize();
                    //candy.Shake(dir, .1f * (40 - dis), .005f * dis);
                }
            }

            ShakeAll();

            GameObject obj = Instantiate(m_DestroyParticle);
            obj.transform.position = transform.position + new Vector3(0, 0, -2);
            Destroy(obj, 3);
        }

        public void ShakeAll()
        {
            foreach (Candy candy in GameControl.m_Current.m_Candies)
            {
                if (!candy.gameObject.activeSelf)
                    continue;

                float dis = Vector3.Distance(candy.transform.position, transform.position);
                if (dis <= 40)
                {
                    Vector3 dir = candy.transform.position - transform.position;
                    dir.z = 0;
                    dir.Normalize();
                    candy.Shake(dir, .1f * (40 - dis), .005f * dis);
                }
            }
        }

        public void EndBall()
        {
            GameControl.m_Current.HandleBallEnd();
            Destroy(gameObject);
        }


    }
}